package com.magicsoft.share.ui.exchange;


import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.magicsoft.share.R;
import com.magicsoft.share.adapter.exchange.GoodsAdapter;
import com.magicsoft.share.base.BaseFragment;
import com.magicsoft.share.bean.exchange.GoodsList;
import com.magicsoft.share.bean.exchange.GoodsSubmit;
import com.magicsoft.share.presenter.exchange.GoodsFragmentPresenter;
import com.magicsoft.share.view.exchange.IGoodsFragmentView;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuRecyclerView;

import java.util.ArrayList;

import butterknife.BindView;

/**
 * A simple {@link Fragment} subclass.
 */
public class GoodsFragment extends BaseFragment<IGoodsFragmentView, GoodsFragmentPresenter> implements IGoodsFragmentView {


    @BindView(R.id.recyclerView)
    SwipeMenuRecyclerView recyclerView;


    private GoodsAdapter goodsAdapter;
    private int mType;
    private int pageNum = 1;
    private int pageSize = 10;
    private boolean isRefresh = true;


    public GoodsFragment() {
        // Required empty public constructor
    }


    @Override
    public int getLayoutId() {
        return R.layout.fragment_goods;
    }

    @Override
    public GoodsFragmentPresenter initPresenter() {
        return new GoodsFragmentPresenter();
    }

    @Override
    protected void initData() {


    }

    @Override
    public void initView() {
        recyclerView.setLayoutManager(new GridLayoutManager(getContext(), 2));

        recyclerView.useDefaultLoadMore();
        recyclerView.setLoadMoreListener(new SwipeMenuRecyclerView.LoadMoreListener() {
            @Override
            public void onLoadMore() {

                pageNum++;
                isRefresh = false;
                mPresenter.goodsList(mType, pageNum, pageSize, isRecommend());
            }
        });

        goodsAdapter = new GoodsAdapter(R.layout.item_goods);
        goodsAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                if (view.getId() == R.id.btn_buy) {
                    GoodsList.ListBean item = goodsAdapter.getItem(position);
                    Intent intent = new Intent(getContext(), SettlementActivity.class);
                    GoodsSubmit goodsSubmit = new GoodsSubmit(item.getId(), item.getGoodsName(), item.getGoodsImg(), item.getPrice(), 1);
                    ArrayList<GoodsSubmit> goodsSubmits = new ArrayList<>();
                    goodsSubmits.add(goodsSubmit);
                    String goodsStr = new Gson().toJson(goodsSubmits);
                    intent.putExtra("goodsStr", goodsStr);
                    intent.putExtra("isDirect", 1);
                    intent.putExtra("orderAmount", item.getPrice());
                    intent.putExtra("tyoe",1);
                    intent.putExtra("GoodsList", goodsSubmits);
                    startActivity(intent);

                }
            }
        });

        goodsAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Intent intent = new Intent(getActivity(), GoodsActivity.class);
                intent.putExtra("goodsId", goodsAdapter.getItem(position).getId());
                startActivity(intent);
            }
        });

        recyclerView.setAdapter(goodsAdapter);


    }

    @Override
    protected void requestData() {
        mPresenter.goodsList(mType, pageNum, pageSize, isRecommend());
    }

    public void refreshData() {
        isRefresh = true;
        pageNum = 1;
        mPresenter.goodsList(mType, pageNum, pageSize, isRecommend());
    }

    public void setType(int type) {
        mType = type;

    }


    @Override
    public void goodsListSuccess(GoodsList data) {


        if (isRefresh) {
            goodsAdapter.setNewData(data.getList());
        } else {
            goodsAdapter.addData(data.getList());
        }

        if (pageNum < data.getPages()) {
            recyclerView.loadMoreFinish(false, true);
        } else if (pageNum == data.getPages()) {
            recyclerView.loadMoreFinish(false, false);
        } else {
            recyclerView.loadMoreFinish(true, false);
        }
    }

    @Override
    public void goodsListFail() {
        if (isRefresh) {
            recyclerView.loadMoreFinish(true, false);
        } else {
            recyclerView.loadMoreFinish(false, false);
        }
    }

    public Integer isRecommend() {
        if (mType == 1) {
            return 1;
        } else {
            return null;
        }

    }


}
